package com.cyt.work.site.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysFile;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.site.dao.BannerDao;
import com.cyt.work.site.model.po.Banner;
import com.cyt.work.site.model.vo.BannerVo;
import com.cyt.work.site.service.BannerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 微官网-轮播图Service层实现类
 *
 * @author rong yuan
 * @date 2019-7-8 17:55:38
 */
@Service("bannerService")
public class BannerServiceImpl extends BaseServiceImpl<Banner> implements BannerService {

    @Resource
    private BannerDao bannerDao;
    @Resource
    private SysFileService sysFileService;

    @Autowired
    public void setBaseDao(BannerDao bannerDao) {
        this.baseDao = bannerDao;
    }

    /**
     * 分页查询
     *
     * @param bannerVo 接受参数对象
     * @param pageNum  分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 17:55:38
     */
    @Override
    public ReturnResult page(BannerVo bannerVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BannerVo> list = bannerDao.selectByCondition(bannerVo);
        PageInfo<BannerVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param bannerVo 参数接受对象
     * @param sysUser  当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 17:55:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(BannerVo bannerVo, SysUser sysUser) {
        if (StrUtil.isEmpty(bannerVo.getId())) {
            Banner banner = new Banner(sysUser);
            banner.setApplyId(bannerVo.getApplyId());
            banner.setTitle(bannerVo.getTitle());
            banner.setBannerType(bannerVo.getBannerType());
            banner.setOrderNum(bannerVo.getOrderNum());
            banner.setPhoto(bannerVo.getPhoto());
            banner.setPath(getFilePath(bannerVo.getPhoto()));
            banner.setContent(bannerVo.getContent());
            banner.setStatus(bannerVo.getStatus());
            int num = bannerDao.insert(banner);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            Banner banner = bannerDao.selectByPrimaryKey(bannerVo.getId());
            banner.setApplyId(bannerVo.getApplyId());
            banner.setTitle(bannerVo.getTitle());
            banner.setBannerType(bannerVo.getBannerType());
            banner.setOrderNum(bannerVo.getOrderNum());
            banner.setPhoto(bannerVo.getPhoto());
            banner.setPath(getFilePath(bannerVo.getPhoto()));
            banner.setContent(bannerVo.getContent());
            banner.setStatus(bannerVo.getStatus());
            banner.setUpdateTime(new Date());
            banner.setUpdateUser(sysUser.getId());
            int num = bannerDao.updateByPrimaryKey(banner);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    private String getFilePath(String ObjectId) {
        SysFileVo sysFileVo = new SysFileVo(ObjectId);
        List<SysFile> sysFileList = sysFileService.listExample(sysFileVo);
        if (sysFileList != null && sysFileList.size() == 1) {
            return sysFileList.get(0).getPath();
        }
        return "";
    }

    /**
     * 根据条件逻辑删除
     *
     * @param bannerVo 参数接受对象
     * @param sysUser  当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 17:55:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(BannerVo bannerVo, SysUser sysUser) {
        //要修改的字段
        Banner banner = new Banner();
        banner.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        banner.setUpdateUser(sysUser.getId());
        banner.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(bannerVo.getId())) {
            criteria.andIn("id", Arrays.asList(bannerVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        bannerDao.updateByExampleSelective(banner, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param bannerVo 参数接受对象
     * @param sysUser  当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 17:55:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(BannerVo bannerVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(bannerVo.getId())) {
            criteria.andIn("id", Arrays.asList(bannerVo.getId().split(",")));
        }
        bannerDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 发布/取消发布
     *
     * @param bannerVo 参数接受对象
     * @param sysUser  当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 15:17:40
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult updateStatus(BannerVo bannerVo, SysUser sysUser) {
        //要修改的字段
        Banner banner = new Banner();
        banner.setStatus(bannerVo.getStatus());
        banner.setUpdateUser(sysUser.getId());
        banner.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(bannerVo.getId())) {
            criteria.andIn("id", Arrays.asList(bannerVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        bannerDao.updateByExampleSelective(banner, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * list查询
     *
     * @param bannerVo 参数接受对象
     * @return List<BannerVo>
     * @author rong yuan
     * @date 2019-7-8 15:17:40
     */
    @Override
    public List<BannerVo> list(BannerVo bannerVo) {
        return bannerDao.selectByCondition(bannerVo);
    }
}